<?php

if (!isset($_GET['op'])) {
    // header('HTTP/1.1 403 ');
    echo '参数错误，未授权的操作';
    return;
}

$op = $_GET['op'];
if ($op == 1) {
$action = '../private/action/register_action.php';
$button = 'Register right now';
$address = 'register.php?op=2';
$btnText = 'login';
$reminder = 'I have already signed up,';

session_start();

$hasError = isset($_SESSION['has_error']) ? $_SESSION['has_error'] : false;
$errorBag = isset($_SESSION['error_bag']) ? $_SESSION['error_bag'] : ['userName' => '', 'password' => '', 'pwdConfirm' => ''];
$inputBag = isset($_SESSION['input_bag']) ? $_SESSION['input_bag'] : ['userName' => ''];

// 销毁session中的错误数据包 （错误数据包是一次性的）
if (isset($_SESSION['has_error']) && isset($_SESSION['input_bag']) && isset($_SESSION['error_bag'])) {
   unset($_SESSION['has_error']);
   unset($_SESSION['input_bag']);
   unset($_SESSION['error_bag']);
}
}


if ($op == 2) {
	$action = '../private/action/login_action.php';
	$button = 'Login';
	$address = 'register.php?op=1';
	$btnText = 'register';
	$reminder = 'No account yet,';

	session_start();
	$hasError = isset($_SESSION['has_error']) ? $_SESSION['has_error'] : false;
	$errorBag = isset($_SESSION['error_bag']) ? $_SESSION['error_bag'] : ['userName' => '', 'password' => ''];

	// 销毁session中的错误数据包 （错误数据包是一次性的）
	if (isset($_SESSION['has_error']) && isset($_SESSION['error_bag'])) {
   		unset($_SESSION['has_error']);
   		unset($_SESSION['error_bag']);
	}
}

?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>Message Board Register</title>
	<link rel="stylesheet" type="text/css" href="css/register.css">
</head>
<body>
	<div id="wrapper">
		<form method="post" action="<?= $action ?>">
			<div class="input">
				<label for="userName">Username</label>
				<input type="text" name="userName" id="userName" placeholder="Please input username" <?php if ($op == 1): ?>value="<?= $inputBag['userName'] ?><?php endif; ?>">
			</div>
			<div class="row">
				<?php if ($hasError) echo $errorBag['userName'] ?>
			</div>
			<div class="input">
				<label for="password">Password</label>
				<input type="password" name="password" id="password" placeholder="Please input password">
			</div>
			<div class="row">
				<?php if ($hasError) echo $errorBag['password'] ?>
			</div>
			<?php if ($op == 1): ?>
			<div class="input">
				<label for="pwdConfirm">Pwdconfirm</label>
				<input type="password" name="pwdConfirm" id="pwdConfirm" placeholder="Please input pwdConfirm">
			</div>
			<div class="row">
				<?php if ($hasError) echo $errorBag['pwdConfirm'] ?>
			</div>
			<?php endif; ?>
			<div class="input">
				<input type="submit" value="<?= $button ?>" class="register">
			</div>
			<span class="login">
				<span><?= $reminder ?></span>
				<a href="<?= $address ?>"><?= $btnText ?></a>
				<span>right now</span>
			</span>
		</form>
	</div>
</body>
</html>